package com.iraeta.controller.kcpd;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.Config;
import com.iraeta.WeiXinConfig;
import com.iraeta.common.AuthAccess;
import com.iraeta.common.Result;
import com.iraeta.common.ServiceException;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.main.Student;
import com.iraeta.service.basic.StudentService;
import com.iraeta.service.sys.ISysFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController(value = "StudentController")
@RequestMapping("student")
public class StudentController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    Config config;
    @Autowired
    WeiXinConfig weiXinConfig;
    @Autowired
    StudentService studentService;
    @Autowired
    private ISysFilesService iSysFilesService;

    @RequestMapping("testok")
    @AuthAccess
    public String testOk(){
        return JSON.toJSONString(Result.success());
    }

    @RequestMapping("testok2")
    @AuthAccess
    public String testOk2(@RequestBody JSONObject jsonObject){
        System.out.println(jsonObject.toJSONString());
        return JSON.toJSONString(Result.error());
    }

    @RequestMapping("exception")
    @AuthAccess
    public String exception(){
        throw new ServiceException("异常了啊");
    }

    @RequestMapping("posttest")
    @AuthAccess
    public String postTest(){
        String url = "http://localhost:8282/student/testok2";
        String body =  JSON.toJSONString(Result.success());
        String res = HttpUtil.post(url, body, 3000);
        return res;
    }

    @RequestMapping("liststudent")
    @AuthAccess
    public String listStudent(@RequestParam(value = "stuId") int stuId){
        logger.info( "liststudent info");
        logger.warn("liststudent warn");
        logger.error("liststudent error");
        logger.debug( "liststudent debug");

        Student student = studentService.getStudent(stuId);
        String studentInfo =  student == null ? "null" : student.toString();

        return config.appKey + config.appSercret + "_" + "config" + "stu[" + studentInfo + "]";
    }

    @RequestMapping("liststudent2")
    @AuthAccess
    public String listStudent(){
        List<Student> student = studentService.getStudentByCondition(null, "1987-05-06", "1989-05-06");
        List<String> nameList = student.stream().map(Student::getName).collect(Collectors.toList());
        return nameList.toString();
    }

    @RequestMapping("config")
    @AuthAccess
    public String config(){
        return config.appKey + "_" + config.appSercret + "_" + config.profile + "_" + weiXinConfig.getAppId() + "_" + weiXinConfig.getCompId() + "_" + weiXinConfig.getAppSecret();
    }

    /*测试程序*/
    @RequestMapping("pagestudent")
    public String pageStudent(@RequestBody JSONObject jo){
        this.pageSet(jo);
        List<Student> studentList = studentService.getStudentByCondition(null, null, null);
        PageInfo<Student> studentPageInfo = this.getPageInfo(studentList);
        return JSON.toJSONString(Result.success(studentPageInfo));
    }

    @RequestMapping("getstudent")
    public String getStudent(@RequestBody JSONObject paramJb){
        int stuId = paramJb.getIntValue("id");
        Student student = studentService.getStudent(stuId);
        return JSON.toJSONString(Result.success(student));
    }

    @RequestMapping("addeditstudent")
    public String addStudent(@RequestBody JSONObject paramJb) throws Exception{
        int stuId = paramJb.getIntValue("id");
        Student student = stuId <= 0 ? new Student() :  studentService.getStudent(stuId);
        this.mapJsonToObj(paramJb, student);
        if(stuId <= 0) student.setId(0);
        if(stuId <= 0){
            studentService.addStudent(student);
            iSysFilesService.updateSysFilesEntityId(stuId, student.getId()); //这里修改上传图片的关联关系
        }else {
            studentService.updateStudent(student);
        }
        Thread.sleep(1500L);
        return JSON.toJSONString(Result.success());
    }

    @RequestMapping("deletestudent")
    public String deleteStudent(@RequestBody JSONObject paramJb){
        int stuId = paramJb.getIntValue("id");
        studentService.deleteStudent(stuId);
        return JSON.toJSONString(Result.success());
    }

    /*测试函数*/
    @RequestMapping("addstudent2")
    public String addStudent2(@RequestParam(value = "stuId") int stuId){
        Student student = new Student();
        student.setAge(35);
        student.setBirth(new Date());
        student.setName("陈宾" + stuId);
        int res = studentService.addStudent(student);
        return String.valueOf(res);
    }
}
